/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package control;

import java.util.TimerTask;
import javax.microedition.lcdui.game.TiledLayer;

/**
 *
 * @author Douglas
 */
public class AnimacaoTile extends TimerTask{

    private boolean movendo = true;
    private boolean adiantando = true;
    private TiledLayer tiledLayer;
    private int indiceTileAnimado;
    private int[] sequencia;
    private int indiceSequencia;

    public AnimacaoTile(TiledLayer tiledLayer, int indiceTileAnimado, int[] sequencia, boolean adiantando) {
        this.tiledLayer = tiledLayer;
        this.indiceTileAnimado = indiceTileAnimado;
        this.sequencia = sequencia;
        this.adiantando = adiantando;
    }

    public void run() {
        if (!this.movendo) {
            return;
        }
        if (adiantando) {
            if (++this.indiceSequencia >= this.sequencia.length) {
                indiceSequencia = 0;
            }
        } else {
            if (--this.indiceSequencia < 0) {
                indiceSequencia = this.sequencia.length - 1;
            }
        }
        this.tiledLayer.setAnimatedTile(this.indiceTileAnimado, this.sequencia[indiceSequencia]);
    }

    public void adiantar() {
        this.adiantando = true;
        this.movendo = true;
    }

    public void voltar() {
        this.adiantando = false;
        this.movendo = true;
    }

    public void setMovendo(boolean movendo) {
        this.movendo = movendo;
    }

}
